package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.wcs.service.system.entity.InterfaceLogDB;
import com.suray.wcs.service.system.entity.TaskDB;
import com.suray.wcs.service.system.mapper.InterfaceLogMapper;
import com.suray.wcs.service.system.service.IInterfaceLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class InterfaceLogServiceImpl extends ServiceImpl<InterfaceLogMapper, InterfaceLogDB> implements IInterfaceLogService {
    @Autowired
    private InterfaceLogMapper interfaceLogMapper;

    @Override
    public List<InterfaceLogDB> getInterfaceLogs(InterfaceLogDB interfaceLogdb) {
        LambdaQueryWrapper<InterfaceLogDB> queryWrapper = new LambdaQueryWrapper();
        if (!StringUtils.isEmpty(interfaceLogdb.getRecipient())) {
            queryWrapper.like(InterfaceLogDB::getRecipient, interfaceLogdb.getRecipient());
        }
        if (!StringUtils.isEmpty(interfaceLogdb.getInterfaceMethodDes())) {
            queryWrapper.like(InterfaceLogDB::getInterfaceMethodDes, interfaceLogdb.getInterfaceMethodDes());
        }
        if (!StringUtils.isEmpty(interfaceLogdb.getInterfaceParamsIn())) {
            queryWrapper.like(InterfaceLogDB::getInterfaceParamsIn, interfaceLogdb.getInterfaceParamsIn());
        }
        if (!StringUtils.isEmpty(interfaceLogdb.getInterfaceParamsRet())) {
            queryWrapper.like(InterfaceLogDB::getInterfaceParamsRet, interfaceLogdb.getInterfaceParamsRet());
        }

        if(!StringUtils.isEmpty(interfaceLogdb.getParams().get("beginTime"))){
            queryWrapper.ge(InterfaceLogDB::getCreateTime, interfaceLogdb.getParams().get("beginTime"));
        }
        if(!StringUtils.isEmpty(interfaceLogdb.getParams().get("endTime"))) {
            queryWrapper.le(InterfaceLogDB::getCreateTime, interfaceLogdb.getParams().get("endTime"));
        }
        if(!StringUtils.isEmpty(interfaceLogdb.getStatus())) {
            queryWrapper.eq(InterfaceLogDB::getStatus, interfaceLogdb.getStatus());
        }
        queryWrapper.orderByDesc(InterfaceLogDB::getSendTime);
        return interfaceLogMapper.selectList(queryWrapper);
    }

    @Override
    public boolean setInterfaceLog(InterfaceLogDB interfaceLogdb) {
        return this.save(interfaceLogdb);
    }

    @Override
    public void deleteThreeDaysIfLog(Integer days) {
        interfaceLogMapper.deleteThreeDaysIfLog(days);
    }

    @Override
    public int deleteLogByIds(Long[] interfaceLogIds) {
        return interfaceLogMapper.deleteLogByIds(interfaceLogIds);
    }

    @Override
    public void cleanLog() {
        interfaceLogMapper.cleanLog();
    }

}
